package com.jmpt.fangbohui.http;

import android.content.Context;
import android.text.TextUtils;

import com.jmpt.fangbohui.utils.DebugLog;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import java.io.File;
import java.io.FileNotFoundException;


public class ApiRequest {
    public static RequestParams getBaseParams() {
        RequestParams params = new RequestParams();
        params.put("os", "android");
        return params;
    }

    public static RequestParams getBaseLoginParams(Context context) {
        RequestParams params = getBaseParams();
//        UserBean user = AccountHelper.getUser();
//        if (user != null && TextUtils.isEmpty(user.token) == false) {
//            params.put("token", user.token);
//            DebugLog.i("token:" + user.token);
//        }
        return params;
    }

    public static RequestHandle register(Context context, String username, String password, String question1, String answer1, String question2, String answer2, String pushUserId, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        params.put("username", username);
        params.put("password", password);
        params.put("question1", question1);
        params.put("answer1", answer1);
        params.put("question2", question2);
        params.put("answer2", answer2);
        if (TextUtils.isEmpty(pushUserId) == false) {
            params.put("push_id", pushUserId);
        }
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.REGISTER), params, handler);
    }

    public static RequestHandle login(Context context, String username, String password, String pushUserId, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        params.put("username", username);
        params.put("password", password);
        if (TextUtils.isEmpty(pushUserId) == false) {
            params.put("push_id", pushUserId);
        }
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.LOGINT), params, handler);
    }

    public static RequestHandle changePassword(Context context, String oldPassword, String password, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        params.put("old_pwd", oldPassword);
        params.put("new_pwd", password);
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.CHANGE_PASSWORD), params, handler);
    }

    public static RequestHandle logout(Context context, String pushUserId, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        if (TextUtils.isEmpty(pushUserId) == false) {
            params.put("push_id", pushUserId);
        }
        return AsyncHttp.post(context, ApiUrl.getAbsoluteUrl(ApiUrl.LOGOUT), params, handler);
    }

    public static RequestHandle getUserInfo(Context context, JsonHttpHandler handler) {
        RequestParams params = getBaseLoginParams(context);
        return AsyncHttp.get(context, ApiUrl.getAbsoluteUrl(ApiUrl.USER_INFO), params, handler);
    }
}
